package com.yzmoney.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Created by LiuJun on 2015/7/14.
 */
public class Functions {
    private static final Log log = LogFactory.getLog(Functions.class);

    public static boolean isDecimal(double value) {
        int i = (int) value;
        double d = value - i;
        if (0 < d) {
            return true;
        }
        return false;
    }

    //获取文件扩展名
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int index = filename.lastIndexOf('.');
            if ((index > -1) && (index < (filename.length() - 1))) {
                return filename.substring(index + 1);
            }
        }
        return filename;
    }

    //获取不带扩展名的文件名
    public static String getFileNameNoExtension(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int index = filename.lastIndexOf('.');
            if ((index > -1) && (index < (filename.length()))) {
                return filename.substring(0, index);
            }
        }
        return filename;
    }

    //获取枚举的索引号
    public static int getEnumIndex(Enum[] enums, Enum e) {
        int index = 0;
        if (e == null) {
            return index;
        }
        for (Enum o : enums) {
            if (e == o) {
                break;
            }
            index++;
        }
        return index;
    }

    public static String urlEncode(String string, String charset) {
        if (charset == null || charset.length() == 0) {
            charset = "utf-8";
        }
        try {
            string = URLEncoder.encode(string, charset);
        } catch (Exception ex) {
            log.warn(JDKStackTrace.getJDKStrack(ex));
        }
        return string;
    }

    public static String urlDecode(String string, String charset) {
        if (string == null || string.length() == 0) {
            return string;
        }
        if (charset == null || charset.length() == 0) {
            charset = "utf-8";
        }
        try {
            string = URLDecoder.decode(string, charset);
        } catch (Exception ex) {
            log.warn(JDKStackTrace.getJDKStrack(ex));
        }
        return string;
    }

    //除以
    public static double divide(double value1, double value2) {
        BigDecimal bd1 = new BigDecimal(value1);
        BigDecimal bd2 = new BigDecimal(value2);
        BigDecimal bd = bd1.divide(bd2);
        return bd.doubleValue();
    }

    //乘以
    public static double multiply(double value1, double value2) {
        BigDecimal bd1 = new BigDecimal(value1);
        BigDecimal bd2 = new BigDecimal(value2);
        BigDecimal bd = bd1.multiply(bd2);
        return bd.doubleValue();
    }

    //截取字符串
    public static String substring(String str, int length) {
        return substring(str, length, null);
    }

    //截取字符串
    public static String substring(String str, int length, String suffix) {
        if (str == null) {
            return null;
        }
        if (length <= 0) {
            return "";
        }
        try {
            if (str.getBytes("GBK").length <= length) {
                return str;
            }
        } catch (Exception ex) {
        }
        StringBuffer sb = new StringBuffer();

        int index = 0;
        char c;
        if (suffix != null) {
            length -= suffix.length();
        }
        while (length > 0) {
            c = str.charAt(index);
            if (c < 128) {
                length--;
            } else {
                length--;
                length--;
            }
            sb.append(c);
            index++;
        }
        if (suffix != null) {
            sb.append(suffix);
        }
        return sb.toString();
    }

}
